package com.xyy.saas.payment.adpater.fumin.config;

import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.dao.model.AreaCode;
import com.xyy.saas.payment.dao.model.FmCodeMap;
import com.xyy.saas.payment.service.AreaCodeService;
import com.xyy.saas.payment.service.FmCodeMapService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author wangtianqi
 */
@Component
public class AreaCodeManager {
    private static Logger LOGGER = LoggerFactory.getLogger(AreaCodeManager.class);

    @Autowired
    private AreaCodeService areaCodeService;

    @Autowired
    private FmCodeMapService fmCodeMapService;

    /**
     * 通过地址查询code - 未匹配到返回空串
     */
    public String getAreaCodeByArea(String area) {
        LOGGER.info("AreaCodeManager#getAreaCodeByArea query area code by Area. area:{}", area);
        Long areaCode = 1l;
        Long preAreaCode = null;

        for(AreaCode item : areaCodeService.queryByPid(areaCode)) {
            Deque<AreaCode> deque = new LinkedList<>();
            AreaCode node = item;
            deque.push(node);
            while (!deque.isEmpty()) {
                node = deque.pop();
                if(area.contains(node.getAreaName())) {
                    areaCode = node.getId();
                    preAreaCode = node.getPid();
                    deque.clear();
                    for(AreaCode nextNode : areaCodeService.queryByPid(node.getId())) {
                        deque.addLast(nextNode);
                    }
                }
            }
        }

        LOGGER.info("AreaCodeManager#getAreaCodeByArea query area code by Area. result areaCode:{}, preAreaCode:{}", areaCode, preAreaCode);
        // 表示未匹配到
        if(null == preAreaCode) {
            return StringUtils.EMPTY;
        }
        // 银行说 地区码超过 6位就取上级地区码
        if(Long.toString(areaCode).length() > 6) {
            if(null == preAreaCode) {
                return StringUtils.EMPTY;
            }
            return Long.toString(preAreaCode);
        }
        return Long.toString(areaCode);
    }

    /**
     * 将业务的Code转换成富民能识别的Code
     */
    public String convert2FmCode(String businessAreaCode) {
        LOGGER.info("AreaCodeManager#convert2FmCode convert to fm code start. businessAreaCode:{}", businessAreaCode);
        if(StringUtils.isBlank(businessAreaCode)) {
            return StringUtils.EMPTY;
        }
        FmCodeMap param = new FmCodeMap();
        param.setFromAreaCode(Long.valueOf(businessAreaCode));
        List<FmCodeMap> fmCodeMaps = fmCodeMapService.queryByParam(param);
        if(fmCodeMaps.isEmpty()) {
            LOGGER.error("AreaCodeManager#convert2FmCode convert fail. not map. businessAreaCode:{}", businessAreaCode);
            // 没有映射关系，将使用原来的
            return businessAreaCode;
        }
        if(fmCodeMaps.size() > 1) {
            LOGGER.error("AreaCodeManager#convert2FmCode convert fail. multiple map. businessAreaCode:{}", businessAreaCode);
            // 多个映射关系，也将使用原来的
            return businessAreaCode;
        }
        FmCodeMap fmCodeMap = fmCodeMaps.get(0);
        LOGGER.info("AreaCodeManager#convert2FmCode convert success. businessAreaCode->fmAreaCode:{}->{}", businessAreaCode, fmCodeMap.getToAreaCode());
        return String.valueOf(fmCodeMap.getToAreaCode());
    }

}
